package top.chmin.project.cloud.builder;

import cn.hutool.core.collection.CollectionUtil;
import org.springframework.stereotype.Component;
import top.chmin.project.cloud.common.entity.column.auto.EntityColumn;
import top.chmin.project.cloud.common.entity.label.value.auto.EntityLabelValue;

/**
 * chmin
 * 2023/12/15 15:21
 * cloud-builder
 */
@Component
public class ControllerBuilder implements Builder {
    @Override
    public void build(BuilderConfig config) {
        String content = """
                package top.chmin.project.cloud.#e.module#.#e.pack#.auto;
                                
                // field import auto

                import jakarta.annotation.Resource;
                import org.springframework.web.bind.annotation.RestController;
                import top.chmin.project.cloud.common.entity.Page;

                /**
                 * chmin
                 * #dateTime#
                 * cloud-builder
                 */
                @RestController
                public class #e.simpleName#Controller implements
                // entity selectInterface auto
                // entity searchInterface auto
                    #e.simpleName#Save,
                    #e.simpleName#UpdateById,
                    #e.simpleName#DeleteById,
                    #e.simpleName#SelectById,
                    #e.simpleName#SelectPage
                {

                    @Resource
                    #e.simpleName#Service service;
                                
                    @Override
                    public int save(#e.simpleName#Save.Param param) {
                        return service.save(param);
                    }

                    @Override
                    public int updateById(#e.simpleName#UpdateById.Param param) {
                        return service.updateById(param);
                    }

                    @Override
                    public int deleteById(#e.simpleName#DeleteById.Param param) {
                        return service.deleteById(param);
                    }

                    @Override
                    public #e.simpleName#SelectById.Result selectById(#e.simpleName#SelectById.Param param) {
                        return service.selectById(param);
                    }

                    @Override
                    public Page<#e.simpleName#SelectPage.Result> selectPage(#e.simpleName#SelectPage.Param param) {
                        return service.selectPage(param);
                    }
                                
                // entity select auto
                                
                // entity search auto
                                
                }
                """;

        save(config, config.getEntity().getClassName() + "Controller", content, true);

        config.getEntityColumnList()
                .forEach(f -> appendJava(config, f,
                        "// field import auto",
                        "import java.util.List;",
                        "import java.util.List;",
                        config.getEntity().getClassName() + "Controller"));
        if (CollectionUtil.isEmpty(config.getEntityLabelValueList())) {
            return;
        }
        for (EntityLabelValue entityLabelValue : config.getEntityLabelValueList()) {
            EntityColumn label = config.getEntityColumnList()
                    .stream().filter(f -> f.getFieldCode().equals(entityLabelValue.getLabelFieldCode()))
                    .findFirst().orElse(null);
            EntityColumn value = config.getEntityColumnList()
                    .stream().filter(f -> f.getFieldCode().equals(entityLabelValue.getValueFieldCode()))
                    .findFirst().orElse(null);
            if (label == null || value == null) {
                continue;
            }

            if ("SELECT".equals(entityLabelValue.getDataApiType())) {
                appendJava(config,
                        "// entity selectInterface auto",
                        "#e.simpleName#%sSelect,".formatted(value.getMethodName()),
                        "// entity selectInterface auto\n    #e.simpleName#%sSelect,".formatted(value.getMethodName()),
                        config.getEntity().getClassName() + "Controller");
            }

            if ("SEARCH".equals(entityLabelValue.getDataApiType())) {
                appendJava(config,
                        "// entity searchInterface auto",
                        "#e.simpleName#%sSearch,".formatted(value.getMethodName()),
                        "// entity searchInterface auto\n    #e.simpleName#%sSearch,".formatted(value.getMethodName()),
                        config.getEntity().getClassName() + "Controller");
            }

            if ("SELECT".equals(entityLabelValue.getDataApiType())) {
                appendJava(config,
                        "// entity select auto",
                        "List<#e.simpleName#%sSelect.Result> select%s(#e.simpleName#%sSelect.Param param)".formatted(value.getMethodName(), value.getMethodName(), value.getMethodName()),
                        """
                                    // entity select auto
                                    @Override
                                    public List<#e.simpleName#%sSelect.Result> select%s(#e.simpleName#%sSelect.Param param) {
                                        return service.select%s(param);
                                    }
                                """.formatted(value.getMethodName(), value.getMethodName(), value.getMethodName(), value.getMethodName()),
                        config.getEntity().getClassName() + "Controller");
            }

            if ("SEARCH".equals(entityLabelValue.getDataApiType())) {
                appendJava(config,
                        "// entity search auto",
                        "List<#e.simpleName#%sSearch.Result> search%s(#e.simpleName#%sSearch.Param param)".formatted(value.getMethodName(), value.getMethodName(), value.getMethodName()),
                        """
                                    // entity search auto
                                    @Override
                                    public List<#e.simpleName#%sSearch.Result> search%s(#e.simpleName#%sSearch.Param param) {
                                        return service.search%s(param);
                                    }
                                """.formatted(value.getMethodName(), value.getMethodName(), value.getMethodName(), value.getMethodName()),
                        config.getEntity().getClassName() + "Controller");
            }
        }

    }
}
